package com.homedroid.tasks;

import java.io.IOException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.homedroid.dal.Client;
import com.homedroid.dal.Parser;
import com.homedroid.dal.User;
import com.homedroid.ui.List1;
import com.homedroid.ui.MyList;

public class GetMyListTask extends AsyncTask<Void, String, Boolean> {
	private Context context;
	private ProgressDialog dialog;
	private Client client;
	private boolean connected = false;
	private User user;

	public GetMyListTask(Context context, User user) throws IOException {

		this.context = context;
		this.user = user;
		dialog = new ProgressDialog(context);
		client = new Client();

	}

	@Override
	protected void onPreExecute() {

		this.dialog.setMessage("Connecting...");
		this.dialog.show();
		Log.i("AsyncTask", "onPreExecute");

	}

	@Override
	protected Boolean doInBackground(Void... params) { 
		boolean result = false;
		try {
			Log.i("AsyncTask", "doInBackground: Creating socket");

			if (client.nsocket.isConnected()) {

				Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
				connected = true;
				publishProgress("Getting Data...");
				Parser parser = new Parser();
				client.SendDataToNetwork(parser.getList(user));
				parser.xmlread(client.ReceiveDataFromNetwork());

			}
		} catch (IOException e) {

			publishProgress("Connecting...");
			Log.i("AsyncTask", "doInBackground: IOException");
			dialog.dismiss();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			Log.i("AsyncTask", "doInBackground: Exception");
			dialog.dismiss();
			result = true;
		} finally {
			try {
				client.nsocket.close();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("tot???");
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("tot?");
			}
			Log.i("AsyncTask", "doInBackground: Finished");
			dialog.dismiss();
			result = true;

		}
		return result;
	}

	@Override
	protected void onProgressUpdate(String... values) {
		if (values.length > 0) {
			Log.i("AsyncTask", "onProgressUpdate: " + values[0]);
			this.dialog.setMessage(values[0]);
			if (!connected) {
				Toast.makeText(context, "No Connection to Server",
						Toast.LENGTH_SHORT).show();

			}
		}
	}

	@Override
	protected void onCancelled() {
		Log.i("AsyncTask", "Cancelled.");
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (result) {
			Log.i("AsyncTask - result=true", "Loading successfull");
//			 Toast.makeText(context, "Loading successfull", Toast.LENGTH_SHORT).show();
			 MyList.done = true;
			 context.startActivity(new Intent(context, MyList.class).putExtra("buffer", List1.product.getDescription(List1.product.getBuffer())));
			 
			 
		} else {
			 Log.i("AsyncTask - result=false", "Loading Error");
			 Toast.makeText(context, "Loading Error", Toast.LENGTH_SHORT).show();
		}

	}

}
